<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class TopicResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'key' => $this->key,
            'type' => $this->type,
            'is_report' => $this->is_report,
            'type_name' => $this->type_name,
            'type_name_formatted' => $this->when($this->is_report, " | {$this->type_name}"),
            'title' => $this->title,
            'title_excerpt' => $this->when($this->resource->relationLoaded('mainFloor'), $this->title_excerpt),
            'view_count' => $this->view_count,
            'comment_count' => $this->comment_count,
            'created_at' => createdAt($this->created_at),
            'last_comment_at' => createdAt(date('Y-m-d H:i:s', $this->last_comment_time)),
            'user' => new UserResource($this->whenLoaded('user')),
            'forum' => new ForumResource($this->whenLoaded('forum')),
            'main_floor' => new CommentResource($this->whenLoaded('mainFloor')),
            'tags' => TagResource::collection($this->whenLoaded('tags')),
        ];
    }

}
